package com.dds.demo.config.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.dds.demo.config.ContextHolder;
import com.dds.demo.config.DynamicDataSource;
import com.dds.demo.util.SpringContextUtil;

public class InterceptorCustom implements HandlerInterceptor {

	@Autowired
	ApplicationContext ctx;
	@Autowired
	Environment env;
	// 默认数据源
	private DataSource defaultDataSource;

	private ConversionService conversionService = new DefaultConversionService();

	private final Logger logger = LoggerFactory.getLogger(InterceptorCustom.class);
	// 数据源配置信息
	private PropertyValues dataSourcePropertyValues;

	/**
	 * 在请求处理之前进行调用（Controller方法调用之前）");
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		getContextHolderInfo(request);
		return true;
	}

	/**
	 * 请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）")
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
	}

	/**
	 * 获取ContextHolder信息
	 * 
	 * @param request
	 */
	public void getContextHolderInfo(HttpServletRequest request) {
		String userId = request.getParameter("userId");
		String dataSource = "";

		if (StringUtils.isEmpty(userId)) {
			dataSource = "test";
		} else {
			if (userId.equals("1")) {
				dataSource = "test1";
			} else if (userId.equals("2")) {
				dataSource = "test2";
			}else if(userId.equals("3")){
				dataSource = "test3";
			}
			
		}
    	ContextHolder.setDataSource(dataSource);
    	ContextHolder.setDataSourceType(dataSource);
	}

}
